package com.cskt.itripbiz.comtroller;

import com.cskt.common.condition.SearchOrderCondition;
import com.cskt.common.condition.ValidateRoomStoreCondition;
import com.cskt.common.constants.ErrorCodeEnum;
import com.cskt.common.vo.*;
import com.cskt.itripbiz.service.HotelOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author mo
 * @Description: 酒店房间订单控制器
 * @date 2020-10-06 14:23
 */
@Api(tags = "酒店房间订单控制器")
@RestController
@RequestMapping(value = "/api/hotelorder")
public class HotelOrderController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private HotelOrderService hotelOrderService;

    public ReturnResult getPreOrderInfo(@RequestBody ValidateRoomStoreCondition condition, HttpServletRequest request){
        //先验证当前用户是否登录
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            //token为空
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_IS_EMPTY);
        }
        String loginUserString = stringRedisTemplate.opsForValue().get(token);

        if (StringUtils.isEmpty(loginUserString)){
            //token为空
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_INVALID);
        }

        if (StringUtils.isEmpty(condition.getHotelId())){
            //hotelid不能为空
            return ReturnResult.error(ErrorCodeEnum.BIZ_HOTELID_IS_EMPTY);
        }

        if (StringUtils.isEmpty(condition.getRoomId())) {
            // roomid 不能为空
            return ReturnResult.error(ErrorCodeEnum.BIZ_ROOMID_IS_EMPTY);
        }
        RoomStoreVO roomStoreVO =
                hotelOrderService.getRoomStoreVOByCondition(condition);
        if (StringUtils.isEmpty(roomStoreVO)) {
            return ReturnResult.error(ErrorCodeEnum.BIZ_NO_ROOM);
        }
        return ReturnResult.ok(roomStoreVO);
    }

    @ApiOperation(value = "根据订单ID查看个人订单详情-房型相关信息", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = ReturnResult.class, notes = "根据订单ID查看个人订单详情-房型相关信 息" + "<p>支持支付类型(roomPayType)：{\"1\":\"在线付\",\"2\":\"线下付\",\"3\":\"不限\"}</p>")
    @RequestMapping(value = "/getpersonalorderroominfo/{orderId}", method =
            RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ReturnResult getPersonalOrderInfo(@ApiParam(required = true,name = "orderId",value = "订单ID")
                                             @PathVariable Long orderId, HttpServletRequest request){
        //先验证当前用户是否登录
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            //token 为空
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_INVALID);
        }
        String loginUserString = stringRedisTemplate.opsForValue().get(token);
        if (StringUtils.isEmpty(loginUserString)){
            //token 失效
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_INVALID);
        }
        if (StringUtils.isEmpty(orderId)){
            return ReturnResult.error(ErrorCodeEnum.BIZ_PARAMETER_IS_EMPTY);
        }

        PersonalHotelOrderVO personalHotelOrderVO = hotelOrderService.getPersonalHotelOrderVOByOrderId(orderId);
        return ReturnResult.ok(personalHotelOrderVO);
    }

    @ApiOperation(value = "分页查询个人订单列表，并分页显示", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = ReturnResult.class, notes = "根据条件查询个人订单列表，并分页显示" +
            "<p>订单类型(orderType)（-1：全部订单 0:旅游订单 1:酒店订单 2：机票订单）：</p>"
            +
            "<p>订单状态(orderStatus)（0：待支付 1:已取消 2:支付成功 3:已消费 4：已点评）:</p>" +
            "<p>对于页面tab条件：</p>" +
            "<p>全部订单（orderStatus：-1）</p>" +
            "<p>未出行（orderStatus：2）</p>" +
            "<p>待付款（orderStatus：0）</p>" +
            "<p>待评论（orderStatus：3）</p>" +
            "<p>已取消（orderStatus：1）</p>")
    @RequestMapping(value = "/getpersonalorderlist", method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ReturnResult getPersonalOrderList(@RequestBody SearchOrderCondition condition, HttpServletRequest request){
        //先验证当前用户是否登录
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            //token 为空
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_IS_EMPTY);
        }
        String loginUserString = stringRedisTemplate.opsForValue().get(token);
        if (StringUtils.isEmpty(loginUserString)){
            //token 失效
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_INVALID);
        }

        Page<ListHotelOrderVO> hotelOrderVOPage = hotelOrderService.getOrderPageByCondition(condition);
        return ReturnResult.ok(hotelOrderVOPage);
    }

    @ApiOperation(value = "根据订单ID获取订单信息", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = ReturnResult.class, notes = "根据订单ID获取订单信息" )
    @RequestMapping(value = "/queryOrderById/{orderId}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ReturnResult queryOrderById(@ApiParam(required = true, name = "orderId", value = "订单ID")
                                       @PathVariable Long orderId,HttpServletRequest request){
        //先验证当前用户是否登录
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            // token 为空
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_IS_EMPTY);
        }
        String loginUserString = stringRedisTemplate.opsForValue().get(token);
        if (StringUtils.isEmpty(loginUserString)) {
            //token 失效
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_INVALID);
        }
        ModifyHotelOrderVO modifyHotelOrderVO =
                hotelOrderService.getModifyHotelOrderVOByOrderId(orderId);
        return ReturnResult.ok(modifyHotelOrderVO);

    }
}
